package cn.odore.org.controller;

import cn.odore.basic.util.AjaxResult;
import cn.odore.basic.util.PageList;
import cn.odore.org.domain.Employee;
import cn.odore.org.query.EmployeeQuery;
import cn.odore.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工接口
 */
@RestController
@RequestMapping("/emp")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @PostMapping
    public PageList<Employee> list(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.list(employeeQuery);
    }

    @GetMapping
    @ApiOperation(value = "查询所有员工",notes = "查询出所有员工数据")
    public List<Employee> getAll(){
        return employeeService.getAll();
    }

    @PutMapping
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            employeeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败" + e.getMessage());
        }
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            employeeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败" + e.getMessage());
        }
    }

}
